<div class="nav-sub w1180">
	<ul class="zoom">
		<li><a class="on" href="/crm/org/list">商户列表</a></li>
		<li><a href="/crm/org/goods-list">商品列表</a></li>
<!-- 		<li><a href="/crm/org/sell-goods">销售商品管理</a></li> -->
	</ul>
</div>
<div class="main-ct w1180">
	<div class="top-bar">
		<div class="custom-search">
			<label>城市
				<select class="category-select" id="city_id">
					<option value="">请选择</option>
					<?php if ($this->city):?>
    				<?php foreach ($this->city as $k=>$v):?>
        				<option value="<?php echo $v['city_id']; ?>"><?php echo $v['city_name']; ?></option>
        			<?php endforeach; ?>
    				<?php endif;?>
				</select>
			</label>
			<label>商户类型
				<select class="category-select" id="catalog_id">
					<option value="">请选择</option>
					<?php if ($this->catalog):?>
    				<?php foreach ($this->catalog as $k=>$v):?>
        				<option value="<?php echo $v['catalog_id']; ?>"><?php echo $v['catalog_name']; ?></option>
        			<?php endforeach; ?>
    				<?php endif;?>
				</select>
			</label>
			<label>是否直营
				<select class="category-select" id="org_is_direct_sale">
					<option value="">请选择</option>
        				<option value="1">非直营</option>
        				<option value="2">直营</option>
				</select>
			</label>
			<label>商户名称<input type="text" class="tp-txt" id="org_name"></label>
			<a href="javascript:search();" class="btn-cs btn-search ml5">查 询</a>
<!-- 			<a href="javascript:;" class="btn-cs btn-green ml5">输出报表</a> -->
		</div>
		<a href="/crm/org/add" class="btn-cs btn-green btn-fillet">新增商户</a>

	</div>
<div id="list-table"></div>
</div>
			
<script type="text/javascript">
    var city_id = $("#city_id").val();
    var catalog_id = $("#catalog_id").val();
    var org_name = $("#org_name").val();
    var org_is_direct_sale = $("#org_is_direct_sale").val();
    var page = 1;
    var addressHtmlInterval = '';
    
    $(document).ready(function(){
    	ajaxListTable();
    });
    
    function search(){
    	city_id = $("#city_id").val();
    	catalog_id = $("#catalog_id").val();
    	org_name = $("#org_name").val();
    	org_is_direct_sale = $("#org_is_direct_sale").val();
    	ajaxListTable();
    }
    
    function paginator(page_no) {
    	page = page_no;
    	ajaxListTable();
    }
    
    function ajaxListTable(){
    	$.ajax({
            type: "post",
            url: "<?php echo $this->url('crm/org',
                array('action'=>'ajax-org-list'));?>",
            dataType:'html',
            data: { "city_id": city_id,
    				"catalog_id": catalog_id,
    				"org_name" : org_name,
    				"org_is_direct_sale" : org_is_direct_sale,
    				"page":page
    		},
            cache: false,
            timeout:10000, 
            error: function(){
              	 alert('请求超时，请重新操作');
               	 isPost = false;
                 return false; 
             }, 
            success: function(html){
            	$("#list-table").html(html);
            }
        });
    }

    $('body').on('click' , '.addr-show' , function(){ 
        var org_id = $(this).data('org-id');
        $("#comm_show_"+org_id).slideToggle(200);

        $.get("/crm/org/ajax-address-list",{'org_id':org_id},function(res){

    		$("#comm_show_"+org_id).html(res);	
    	});

       //$("#address-list-show").treetable();
    
    }); 
    

    $('body').on('click' , '.edit-address' , function(){ 
        var addressId = $(this).data('address-id');
        addressHtmlInterval = $(this).data('address-html-interval');
        
        edit(this,'/crm/org/update-address',addressId,'field');
     }); 

function edit(obj, url, id, field){
    var tag = $(obj).children().first().length;
    if(tag>0 && $(obj).children().first()[0].tagName.toLowerCase()=="input"){
        return false;
    }

    
    //新加的
    if($(obj).children('input').length > 0){
        return false;
    }
    var tdNode = $(obj);
    var value = $(obj).html();
    //var width = ($(obj)[0].offsetWidth+12)+'px';
    var input = $("<input type='text'/>");
    $(obj).html('');
    $(obj).append(input);
    input[0].focus();
    //input.css("width",width);
    input.css("border-width", "0px");
    input.css("width",$(obj).css('width'));
    input.val(value);
    //触发焦点事件
    input.trigger('focus');
    input.trigger('select');

    input.blur(function(){

        var newValue = input.val();
        newValue = newValue.replace(/[-]/g,"");
        if(newValue.length>0){
            
            $.get(url,{id:id, value:newValue, field:field},function(res){
                if(res.result){
                    $(obj).html(addressHtmlInterval + newValue);
                }else{

                    alert(res.reason);
                	$(obj).html(value);
                }
               
            },"json");

            
        }else{
            $(obj).html(value);
        }
    }); 
} 

</script>